package org.tony.web.spider;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

public class HttpClientDemo
{

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    System.setProperty("http.proxyHost", "10.32.235.41");
    System.setProperty("http.proxyPort", "8080");
    HttpHost proxy = new HttpHost("10.32.235.41", 8080);
    HttpClient hc = new DefaultHttpClient();
    hc.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    hc.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0");
    HttpGet get = new HttpGet("http://tieba.baidu.com/f?kw=%CA%A5%D3%FC");
    try
    {
      HttpResponse resp = hc.execute(get);
      HttpEntity entity = resp.getEntity();
      InputStream is = entity.getContent();
      //      byte[] b = new byte[1024];
      //      while (is.read(b) != -1)
      //      {
      //        System.out.println(new String(b, "UTF-8"));
      //      }
      //      is.close();
      System.out.println(EntityUtils.toString(entity));
    }
    catch (ClientProtocolException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    finally
    {
      get.releaseConnection();
    }

    System.out.println("中文 。。。");
  }

}
